/**
 * 项目名称:已知字符串 ”this is a test of java"
 *
 * @Author: 夏国倩
 * @Date: 2023/9/19 15:43
 * @Copyright
 */
package String2;
import java.util.ArrayList;
public class StringTest6 {
    public static void main(String[] args) {
        StringTest6 test1=new StringTest6();
        test1.counter1();
        test1.counter2();
        test1.counter3();
        test1.counter4();
        test1.counter5();
    }

    //统计该字符串中字母s出现的次数
    public void counter1() {
        String str="this is a test of java";
        String b="s";
        int counter = 0;
        String[] temp=str.split(b);
        for (int i=0;i<temp.length;i++){
                counter++;
        }
        System.out.println("该字符串中字母s出现的次数为："+(counter-1)+"次");
    }

    //取出字符串“test”
    public void counter2(){
        String str="this is a test of java";
        String temp1=str.substring(10,14);
        System.out.println(temp1);
    }

    //用多种方式将本字符串复制到一个字符数组Char[] str中
    public void counter3(){
        String str1="this is a test of java";
        char[] str=new char[str1.length()];
        str1.getChars(0,str1.length(),str,0);
        for (char ar:str) {
            System.out.println(str);
        }
    }

    //将字符串中每个单词的第一个字母变成大写，变化后输出到控制台
    public void counter4(){
        String str = "this is a test of java";
        String[] a = str.split(" ");
        String a2 = new String();
        for (int i = 0;i< a.length;i++){
            a[i] = a[i].substring(0,1).toUpperCase()+a[i].substring(1);
            if(i == a.length-1){
                a2 = a2+a[i];
            }else {
                a2 = a2+a[i]+" ";
            }
        }
        System.out.println(a2);

    }

    //将本字符串转换成一个字符串数组，要求每个数组元素都是一个有意义的英文单词，并输出到控制台
    public void counter5(){
        ArrayList<String> list = new ArrayList<String>();
        list.add("this");
        list.add("is");
        list.add("a");
        list.add("test");
        list.add("of");
        list.add("java");
        System.out.println(list);
    }
}
